﻿using System;
using System.Drawing;
using System.Windows.Forms;
using System.Threading;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ArchiveRecoveryProgram;

namespace ArchiveRecoveryProgram
{
    public partial class InteractiveForm : Form
    {
        private Thread dirWatcherThread;

        private bool isDirWatcherRunning;

        public delegate void TestLog();
        public delegate void AppendLog(string str);
        public AppendLog dirWatcherDelegate;
        public TestLog testDelegate;

        public InteractiveForm()
        {
            InitializeComponent();
            dirWatcherDelegate = new AppendLog(AppendRichTextBox);
            testDelegate = new TestLog(TestAppendRichTextBox);
            isDirWatcherRunning = false;
            richTextBox1.ReadOnly = true;
        }

        public void TestAppendRichTextBox()
        {
            richTextBox1.Text += "success" + Environment.NewLine;
        }

        public void AppendRichTextBox(string str)
        {
            if (InvokeRequired)
            {
                Invoke(new Action(() =>
                {
                
                    richTextBox1.Text += str;
                }));
            }
            else
            {
                richTextBox1.Text += str;
            }
        }

        private void InteractiveForm_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            bool res = DirWatcher.OverWriteSourceFiles();
            if (res)
                logInfo("存档还原成功");
            else
                logInfo("存档还原失败！");
        }

        private void button2_Click(object sender, EventArgs e)
        {
            bool res = DirWatcher.Init();
            if(res)
                logInfo("存档备份成功");
            else
                logInfo("存档备份失败！");
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void richTextBox1_TextChanged(object sender, EventArgs e)
        {
            int maxLine = 7;//最大显示行数
            if (richTextBox1.Lines.Length > maxLine)
            {
                int curLines = richTextBox1.Lines.Length - maxLine;
                string[] lines = richTextBox1.Lines;
                Array.Copy(lines, curLines, lines, 0, maxLine);
                Array.Resize(ref lines, maxLine);
                richTextBox1.Lines = lines;
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if(!isDirWatcherRunning)
            {
                isDirWatcherRunning = true;
                dirWatcherThread = new Thread(StartDirWatcher);
                dirWatcherThread.IsBackground = true;
                dirWatcherThread.Start();
            }
            else
            {
                logInfo("程序已经开始运行！");
            }
        }

        private void StartDirWatcher()
        {
            DirWatcher dirWatcher = new DirWatcher(this); // 创建 DirWatcher
            dirWatcher.Run();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            richTextBox1.Text += "test" + Environment.NewLine;
        }

        private void logInfo(String str)
        {
            DateTime currentTime = DateTime.Now;
            string formattedTime = currentTime.ToString("yyyy-MM-dd HH:mm:ss");
            string logMessage = formattedTime + ":" + str + Environment.NewLine;

            richTextBox1.Text += logMessage;
        }
    }
}
